package bb.lanxing.lib.devices.ble.device;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.content.Intent;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.DeviceContext;
import bb.lanxing.lib.devices.base.SimpleBluetoothGattCallbackCompat;
import bb.lanxing.lib.devices.ble.BLEAttributes;
import bb.lanxing.lib.devices.ble.BleCons;
import bb.lanxing.lib.devices.ble.heartrate.HeartRateBeltController;
import bb.lanxing.lib.devices.core.ble.AbsBleDevice;
import bb.lanxing.lib.devices.core.ble.SpecificationAttributes;
import bb.lanxing.lib.devices.core.provider.DataNotifier;
import java.nio.ByteBuffer;
import java.util.UUID;
import org.apache.commons.cli.HelpFormatter;

public class StandardHeartrateBeltDevice extends AbsBleDevice implements HeartRateBeltController {
    private BluetoothGattCharacteristic heartRateWarnNotify;
    private BluetoothGattCharacteristic heartRateWarnWrite;
    public static final UUID BLE_HEART_RATE_SERVICE = UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb");
    public static final UUID BLE_HEART_RATE_BELT_SERVICE = UUID.fromString("0000fd00-0000-1000-8000-00805f9b34fb");
    public static final UUID BLE_HEART_RATE_MEASUREMENT = UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb");
    public static final UUID BLE_HEART_RATE_WARN_NOTIY = UUID.fromString(BLEAttributes.BLE_HEART_RATE_WARN_NOTIFY);
    public static final UUID BLE_HEART_RATE_WARN_WRITE = UUID.fromString(BLEAttributes.BLE_HEART_RATE_WARN_WRITE);

    public StandardHeartrateBeltDevice(Context context, SmartDevice smartDevice) {
        super(smartDevice);
        init(context, smartDevice.getAddress(), new HeartateBeltBluetoothGattCallback(context, this));
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onServicesDiscovered() {
        super.onServicesDiscovered();
        BluetoothGattService service = mBluetoothGatt.getService(BLE_HEART_RATE_SERVICE);
        if (service != null) {
            BluetoothGattCharacteristic characteristic = service.getCharacteristic(BLE_HEART_RATE_MEASUREMENT);
            mBluetoothGatt.readCharacteristic(characteristic);
            setCharacteristicNotification(characteristic, true);
        }
        for (BluetoothGattService bluetoothGattService : mBluetoothGatt.getServices()) {
            if (bluetoothGattService.getUuid().toString().split(HelpFormatter.DEFAULT_OPT_PREFIX)[0].contains(BLEAttributes.BLE_HEART_RATE_BELT_SERVICE_KEY)) {
                for (BluetoothGattCharacteristic bluetoothGattCharacteristic : bluetoothGattService.getCharacteristics()) {
                    if (bluetoothGattCharacteristic.getUuid().toString().contains(BLEAttributes.BLE_HEART_RATE_WARN_NOTIFY_KEY)) {
                        heartRateWarnNotify = bluetoothGattCharacteristic;
                        setCharacteristicNotification(bluetoothGattCharacteristic, true);
                    }
                    if (bluetoothGattCharacteristic.getUuid().toString().contains(BLEAttributes.BLE_HEART_RATE_WARN_WRITE_KEY)) {
                        heartRateWarnWrite = bluetoothGattCharacteristic;
                    }
                }
            }
        }
    }

    @Override
    public boolean setWarnHr(int i) {
        ByteBuffer allocate = ByteBuffer.allocate(5);
        byte b = (byte) i;
        allocate.put((byte) -78);
        allocate.put((byte) 5);
        allocate.put((byte) 9);
        allocate.put(b);
        allocate.put((byte) ((-64) + b));
        return enqueue(AbsBleDevice.Request.newWriteRequest(heartRateWarnWrite, allocate.array()));
    }

    @Override
    public boolean closeWarnHr() {
        ByteBuffer allocate = ByteBuffer.allocate(4);
        allocate.put((byte) -78);
        allocate.put((byte) 4);
        allocate.put((byte) 11);
        allocate.put((byte) -63);
        return enqueue(AbsBleDevice.Request.newWriteRequest(heartRateWarnWrite, allocate.array()));
    }

    @Override
    public void initWarnHr() {
        ByteBuffer allocate = ByteBuffer.allocate(4);
        allocate.put((byte) -78);
        allocate.put((byte) 4);
        allocate.put((byte) 10);
        allocate.put((byte) -64);
        enqueue(AbsBleDevice.Request.newWriteRequest(heartRateWarnWrite, allocate.array()));
    }


    private static class HeartateBeltBluetoothGattCallback extends SimpleBluetoothGattCallbackCompat<StandardHeartrateBeltDevice> {
        private static final String TAG = "HeartateBeltBluetoothGattCallback";
        Context mContext;

        HeartateBeltBluetoothGattCallback(Context context, StandardHeartrateBeltDevice standardHeartrateBeltDevice) {
            super(standardHeartrateBeltDevice);
            mContext = context;
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
            super.onCharacteristicRead(bluetoothGatt, bluetoothGattCharacteristic, i);
            if (bluetoothGattCharacteristic.getUuid().equals(SpecificationAttributes.BLE_HEART_RATE_BODY_LOCATION_CHARACTERISTIC)) {
                onReadHearRateBodyLocation(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));
            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            int intValue;
            super.onCharacteristicChanged(bluetoothGatt, bluetoothGattCharacteristic);
            UUID uuid = bluetoothGattCharacteristic.getUuid();
            int type = mClientRef.getType();
            boolean z = false;
            if (StandardHeartrateBeltDevice.BLE_HEART_RATE_MEASUREMENT.equals(uuid)) {
                if ((bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0) & 1) == 0) {
                    intValue = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                } else {
                    intValue = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 1);
                }
                DataNotifier.notifyHeartrateData(type, mClientRef.getAddress(), intValue);
            } else if (uuid.toString().split(HelpFormatter.DEFAULT_OPT_PREFIX)[0].contains(BLEAttributes.BLE_HEART_RATE_WARN_NOTIFY_KEY)) {
                Intent intent = new Intent();
                bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
                bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                int intValue2 = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 2);
                if (intValue2 == 9) {
                    int intValue3 = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 3);
                    bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 4);
                    intent.setAction(BleCons.ACTION_HERATRATE_BELT_WARN_SET_STATUS);
                    if (intValue3 == 1) {
                        z = true;
                    }
                    intent.putExtra(BleCons.EXTRA_HERATRATE_BELT_WARN_SET_STATUS, z);
                    mContext.sendBroadcast(intent);
                } else if (intValue2 != 10) {
                    if (intValue2 != 11) {
                        return;
                    }
                    int intValue4 = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 3);
                    bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 4);
                    intent.setAction(BleCons.ACTION_HERATRATE_BELT_WARN_CLOSE_STATUS);
                    if (intValue4 == 1) {
                        z = true;
                    }
                    intent.putExtra(BleCons.EXTRA_HERATRATE_BELT_WARN_CLOSE_STATUS, z);
                    mContext.sendBroadcast(intent);
                } else {
                    int intValue5 = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 3);
                    int intValue6 = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 4);
                    bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 5);
                    intent.setAction(BleCons.ACTION_HERATRATE_BELT_WARN);
                    intent.putExtra(BleCons.EXTRA_HERATRATE_BELT_WARN, intValue6 + "");
                    if (intValue5 == 1) {
                        z = true;
                    }
                    intent.putExtra(BleCons.EXTRA_HERATRATE_BELT_WARN_STATUS, z);
                    mContext.sendBroadcast(intent);
                }
            }
        }

        protected void onReadHearRateBodyLocation(int i) {
            if (mClientRef != null) {
                mClientRef.setHeartRateBodyLocation(i);
            }
            Context applicationContext = DeviceContext.getApplicationContext();
            if (applicationContext == null) {
                return;
            }
            Intent intent = new Intent(BleCons.ACTION_HEARTRATE_BODY_LOCATION_RESULT);
            intent.putExtra(BleCons.EXTRA_DEVICE_TYPE, mClientRef.getType());
            intent.putExtra(BleCons.EXTRA_DEVICE_ADDRESS, mClientRef.getAddress());
            intent.setPackage(DeviceContext.getApplicationContext().getPackageName());
            intent.putExtra(BleCons.ACTION_HEARTRATE_BODY_LOCATION_DATA, i);
            applicationContext.sendBroadcast(intent);
            mClientRef.d("BLocationSHB: " + i);
        }
    }
}
